The Shift Cipher

One of the oldest known ciphers is known as Caesar's cipher. Julius Caesar encrypted his messages by shifting every letter of the alphabet three spaces forward and looping back when the end of the alphabet is reached. Consequently, A would be mapped to D and Z would be mapped to C.

An immediate problem with this cipher is the lack of a key - the shift amount is always the same. A natural extension of the cipher would then be to let the shift amount vary, turning it into a key whose possible values are the numbers between 0 and 25. Therefore, the key space is .

An encryption algorithm would take a plaintext , shift its letters forwards by positions and spit out a ciphertext . In contrast, a decryption algorithm would take the ciphertext and shift its letters backwards by places to retrieve the original plaintext. If we map the alphabet to the set (, etc.), a more mathematical description is obtained. Encryption of any message () using the key is given by

The notation is the remainder of upon division by where and denotes concatenation and not multiplication. Decryption of a cyphertext using a key would then be given by

It is only natural to now ask, is this cipher secure? And the simple answer is no. There are only 26 possible keys and so the key-space is not sufficiently big. You can even go through all 26 possible keys with a given ciphertext by hand and check which resulting plaintext makes sense. Most likely, there will be only one and so you would have recovered the original message.

Another method to crack this cipher is by using frequency analysis. Since the shift cipher is a one-to-one mapping on a letter-by-letter basis, the frequency distribution of letters is preserved. For example, the most common letter in English is the letter "e". If we analyse the ciphertext and discover that the most common letter there is "g", then we know that most likely the letter "g" is the letter "e" encrypted with the given key. From this we can calculate the key to be 2 (however, the plaintext, and therefore the ciphertext, may actually deviate from this distribution, so it is not with 100% certainty that the key is 2). We can also perform the same procedure with the rest of the letters in the ciphertext and retrieve the original plaintext. This process can also be automated with some math.

Let's once again map the alphabet with the integers 0 through 25 and also this time let () denote the frequency of the th letter. Using the above table, we can calculate that

Now, let denote the frequency of the th letter in the ciphertext - this is just equal to the number of occurrences of the th letter divided by the length of the ciphertext. If the key is , then should be approximately equal to , since the th letter gets mapped to the th letter (technically, these should be , but that's too cumbersome to write here). Therefore, if we compute

for every value of , then should be approximately equal to 0.065, where is the actual key. For all , would be different from 0.065. This ultimately leads to a way to recover the original key that is fairly easy to automate.

The Vigenère Cipher

This cipher is a more advanced version of the shift cipher. It is a poly-alphabetic shift cipher. Unlike the previous ciphers, it does not define a fixed mapping on a letter-by-letter basis. Instead, it maps blocks of letters whose size depends on the key length. For example, ab could be mapped to xy, ac to zt, and aa to bc. Moreover, identical blocks will be mapped to different blocks depending on their relative position in the plaintext. ab could once be mapped to xy, but then when ab appears again, it may be mapped to ci.

In the Vigenère cipher the key is no longer a single number, but rather a string of letters, where each letter is again mapped to the integers . The key is then repeatedly overlaid with the plaintext and each letter in the plaintext is shifted by the amount denoted by the key letter it has been matched with.

Plaintext:  the golden sun shone brightly, bathing the beach in its warm sunlight
Key:        cok ecokec oke cokec okecokec, okecoke cok ecoke co kec okec okecokec
Ciphertext: vvo kqznip ger uvyrg pbmivdpa, pkxjwxk vvo fgoml kb sxu kkvo gernwqlv

Given a known key length, also called a period, , a ciphertext can be divided into parts, each with length . Therefore, ciphertext characters with the same relative position in each of these groups with length would have all been encrypted using the same shift amount. In the above example, for the groups theg and olde, t and o would have both been encrypted with c, h and l with o and so on. Such characters are said to comprise a stream. Stated in a more mathematical way, for all , the ciphertext characters have all been encrypted by shifting the corresponding plaintext character by positions, where is the th character in the key . It is now possible to use frequency analysis on each stream and check what shift amount yields the correct probability distribution.

If the period is not known, it may be possible to determine it by using Kasiski's method. Initially, you must identify repeated patterns of length 2-3 characters. Kasiski observed that the distance between these repeated patterns (given that they are not coincidental) is a multiple of the period . In the above example, the distance between the two vvos is 32 which is 8 times the period 4.

There is also a more automatable (if this is even a word) approach. Recall that, given a period , the ciphertext characters in the first stream are the upshot of encrypting the corresponding plaintext characters with the same shift amount. Therefore, the frequencies of the characters in the stream will be close to the character frequencies in the English language in some shifted order.

If we let denote the observed frequency of the th letter in the stream (), we would expect that , where is the shift amount and is the frequency of the th letter of the alphabet in a standard English text. Therefore, the sequence is simply the sequence shifted by .

Referring back to previous analysis, we get that

We can easily find the period . For every and the stream we can define

When , it is expected that . In the rest of the cases, we would expect that the character distribution in the stream is fairly uniform (recall that the Vigenère cipher smooths out character distributions) and so

Ergo, the smallest value , for which , is likely the period . This can be further validated by performing the same procedure on the subsequent streams in the ciphertext such as and so on.